package com.qire.manhua.view.attributeBinder;

import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.qire.common.basic.ObjectUtil;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnPageChangeListener;

import androidx.databinding.BindingAdapter;
import androidx.viewpager.widget.ViewPager;

/**
 * 第三方控件事件扩展绑定适配器
 */
public class ThirdPartyViewEventAdapter {

    @BindingAdapter("onLoadMore")
    public static void setOnLoadMoreListener(SmartRefreshLayout layout, OnLoadMoreListener listener){
        layout.setOnLoadMoreListener(listener);
    }
    @BindingAdapter(value = {"onRefresh"},requireAll = false)
    public static void setOnRefreshListener(SmartRefreshLayout layout, OnRefreshListener onRefreshListener){
        layout.setOnRefreshListener(onRefreshListener);
    }

    @BindingAdapter(value = {"onPageScrolled", "onPageSelected", "onPageScrollStateChanged"}, requireAll = false)
    public static void addOnPageChangeListener(Banner banner, OnPageScrolled onPageScrolled, OnPageSelected onPageSelected,
                                               OnPageScrollStateChanged onPageScrollStateChanged) {
        banner.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if(ObjectUtil.nonNull(onPageScrolled)) {
                    onPageScrolled.onPageScrolled(position, positionOffset, positionOffsetPixels);
                }
            }

            @Override
            public void onPageSelected(int position) {
                if(ObjectUtil.nonNull(onPageSelected)) {
                    onPageSelected.onPageSelected(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if(ObjectUtil.nonNull(onPageScrollStateChanged)) {
                    onPageScrollStateChanged.onPageScrollStateChanged(state);
                }
            }
        });
    }

    @BindingAdapter(value = {"onPageScrolled", "onPageSelected", "onPageScrollStateChanged"}, requireAll = false)
    public static void addOnPageChangeListener(SmartTabLayout banner, OnPageScrolled onPageScrolled, OnPageSelected onPageSelected,
                                               OnPageScrollStateChanged onPageScrollStateChanged) {
        banner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if(ObjectUtil.nonNull(onPageScrolled)) {
                    onPageScrolled.onPageScrolled(position, positionOffset, positionOffsetPixels);
                }
            }

            @Override
            public void onPageSelected(int position) {
                if(ObjectUtil.nonNull(onPageSelected)) {
                    onPageSelected.onPageSelected(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if(ObjectUtil.nonNull(onPageScrollStateChanged)) {
                    onPageScrollStateChanged.onPageScrollStateChanged(state);
                }
            }
        });
    }

    public interface OnPageScrolled{
        void onPageScrolled(int position, float positionOffset, int positionOffsetPixels);
    }

    public interface OnPageSelected{
        void onPageSelected(int position);
    }

    public interface OnPageScrollStateChanged{
        void onPageScrollStateChanged(int state);
    }

}
